package com.after.saas.common.utils;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.useragent.Browser;
import cn.hutool.http.useragent.UserAgent;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

/**
 * @Classname AppUtil
 * @Description TODO
 * @Date 2021/4/26 16:29
 * @Created by hjchen
 */

@Slf4j
public class AppUtil {

    public static HttpServletRequest getRequest() {
        ServletRequestAttributes attrs = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        if (ObjectUtil.isEmpty(attrs)) return null;
        HttpServletRequest request = attrs.getRequest();
        return request;
    }

    public static String getBrowser() {
        return getRequest().getHeader("User-Agent");
    }
    /**
     * getRemoteIP:获取远程请求客户端的外网IP <br/>
     *
     * @return ip 外网ip<br/>
     */
    public static String getRemoteIP() {
        HttpServletRequest request = getRequest();
        assert request != null;
        String ip = request.getHeader("x-forwarded-for");
        if (StrUtil.isEmpty(ip) || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if (StrUtil.isEmpty(ip) || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if (StrUtil.isEmpty(ip) || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
        }
        return ip;
    }

}